package com.fjw.car_send;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

public class TouchPad1 extends TouchPadBase {


    private TouchArea[] mTouchAreas = new TouchArea[]{
            new TouchArea(326, 201, 155, 129, TouchAreaType.FORWARD),
            new TouchArea(326, 343, 155, 129, TouchAreaType.BACKWARD),
            new TouchArea(14, 516, 155, 129, TouchAreaType.FORWARD),
            new TouchArea(14, 657, 155, 129, TouchAreaType.BACKWARD),
            new TouchArea(82 - 40, 798, 55 + 40 * 2, 137, TouchAreaType.DRIVE_LEVEL1),
            new TouchArea(217 - 40, 798, 55 + 40 * 2, 137, TouchAreaType.DRIVE_LEVEL2),
            new TouchArea(342 - 40, 798, 55 + 40 * 2, 137, TouchAreaType.DRIVE_LEVEL3),
            new TouchArea(951, 42, 128, 128, TouchAreaType.AUDIO),
            new TouchArea(671, 312, 295, 155, TouchAreaType.LEFT),
            new TouchArea(995, 312, 295, 155, TouchAreaType.RIGHT),
            new TouchArea(671, 567, 295, 155, TouchAreaType.LEFT),
            new TouchArea(995, 567, 295, 155, TouchAreaType.RIGHT),
            new TouchArea(1648, 155, 93, 167, TouchAreaType.LEFT),
            new TouchArea(1924, 166, 98, 164, TouchAreaType.RIGHT),
            new TouchArea(1628, 470, 158, 166, TouchAreaType.TURN_ROUND),
            new TouchArea(1916, 484, 116, 166, TouchAreaType.FORWARD),
            new TouchArea(1610, 777, 83, 124, TouchAreaType.FORWARD_RIGHT),
            new TouchArea(1813, 772, 80, 123, TouchAreaType.FORWARD_LEFT),
            new TouchArea(1976, 779, 73, 104, TouchAreaType.FORWARD),
            new TouchArea(2113, 781, 73, 104, TouchAreaType.BACKWARD),
    };

    final String LOG_TAG = "car_send_touchpad1";

    public TouchPad1(Context context) {
        super(context);
    }

    public TouchPad1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchPad1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TouchPad1(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void doDraw(Canvas canvas) {
        // 在这里进行绘制
        if (canvas == null) {
            return;
        }
        canvas.drawColor(Color.WHITE);
        drawBG(canvas);
        // debugDrawAllAreas(canvas);
        drawTouchedAreas(canvas);
        drawSpeed(canvas);
    }

    private void drawBG(Canvas canvas) {
        int screenWidth = canvas.getWidth();
        int screenHeight = canvas.getHeight();
        Drawable drawable = MainActivity.getInstance().getDrawable(R.drawable.bg);
        drawable.setBounds(0, 0, screenWidth, screenHeight);
        drawable.draw(canvas);
    }

    private void debugDrawAllAreas(Canvas canvas) {
        for (int i = 0; i < mTouchAreas.length; i++) {
            TouchArea touchArea = mTouchAreas[i];
            RectF rect = touchArea.rect;
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(getResources().getColor(R.color.touchArea));
            // mPaint.setColor(Color.RED);
            canvas.drawRect(rect, mPaint);
        }
    }

    private void drawTouchedAreas(Canvas canvas) {
        for (int i = 0; i < mTouchAreas.length; i++) {
            TouchArea touchArea = mTouchAreas[i];
            if (touchArea.isTouched()) {
                RectF rect = touchArea.rect;
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setColor(getResources().getColor(R.color.touchArea));
                canvas.drawRect(rect, mPaint);
            }
        }
    }

    private void drawSpeed(Canvas canvas) {

        mPaint.setTextSize(40);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.speedFillColor));
        int speed = 0;
        switch (Math.abs(mFBLevel)) {
            case 0:
                speed = 0;
                break;
            case 1:
                speed = 30;
                break;
            case 2:
                speed = 70;
                break;
            case 3:
                speed = 100;
                break;
        }
        // 绘制居中文字
        RectF rect = new RectF(0, 0, 503, 190);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);
        canvas.drawRect(rect, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.speedFillColor));
        mPaint.setTextAlign(Paint.Align.CENTER);
        int height = 150;
        mPaint.setTextSize(height);
        String text = Integer.toString(speed);
        canvas.drawText(text, rect.centerX(), rect.centerY() + height / 2, mPaint);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(getResources().getColor(R.color.speedBorderColor));
        canvas.drawText(text, rect.centerX(), rect.centerY() + height / 2, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getActionMasked();
        final int pointerCount = event.getPointerCount();
        if (pointerCount >= 4) {
            MainActivity.getInstance().setActivityIndex(0);
        }

        for (int i = 0; i < pointerCount; i++) {
            int id = event.getPointerId(i);
            float x = event.getX(i);
            float y = event.getY(i);
            if (action == MotionEvent.ACTION_DOWN ||
                    (action == MotionEvent.ACTION_POINTER_DOWN &&
                            i == event.getActionIndex())) {
                Log.d(LOG_TAG, "DOWN");
                for (TouchArea touchArea : mTouchAreas) {
                    if (touchArea.areaCheck(x, y, id)) {
                        switch (touchArea.type) {
                            case FORWARD:
                                mFBLevel = Math.abs(mLastFBLevel);
                                mLastFBLevel = mFBLevel;
                                break;
                            case BACKWARD:
                                mFBLevel = -Math.abs(mLastFBLevel);
                                mLastFBLevel = mFBLevel;
                                break;
                            case LEFT:
                                mLRLevel = -100;
                                break;
                            case RIGHT:
                                mLRLevel = 100;
                                break;
                            case FORWARD_LEFT:
                                mFBLevel = Math.abs(mLastFBLevel);
                                mLRLevel = -100;
                                break;
                            case FORWARD_RIGHT:
                                mFBLevel = Math.abs(mLastFBLevel);
                                mLRLevel = 100;
                                break;
                            case AUDIO:
                                mBeep = mLastBeep + 1;
                                mLastBeep++;
                                mLastBeep = mLastBeep % MAX_BEEP_INDEX;
                                break;
                            case DRIVE_LEVEL1:
                                if (mLastFBLevel >= 0) {
                                    mLastFBLevel = 1;
                                } else {
                                    mLastFBLevel = -1;
                                }
                                mFBLevel = mLastFBLevel;
                                break;
                            case DRIVE_LEVEL2:
                                if (mLastFBLevel >= 0) {
                                    mLastFBLevel = 2;
                                } else {
                                    mLastFBLevel = -2;
                                }
                                mFBLevel = mLastFBLevel;
                                break;
                            case DRIVE_LEVEL3:
                                if (mLastFBLevel >= 0) {
                                    mLastFBLevel = 3;
                                } else {
                                    mLastFBLevel = -3;
                                }
                                mFBLevel = mLastFBLevel;
                                break;
                            case TURN_ROUND:
                                startTurnRound();
                                return true;
                        }
                    }
                }
                sendData();
            } else if (action == MotionEvent.ACTION_MOVE) {
                Log.d(LOG_TAG, "MOVE");
            } else if (action == MotionEvent.ACTION_POINTER_UP && i == event.getActionIndex()) {
                Log.d(LOG_TAG, "UP");
                for (TouchArea touchArea : mTouchAreas) {
                    if (touchArea.getEventId() == id) {
                        touchArea.release();
                        switch (touchArea.type) {
                            case FORWARD:
                            case BACKWARD:
                            case DRIVE_LEVEL1:
                            case DRIVE_LEVEL2:
                            case DRIVE_LEVEL3:
                                mFBLevel = 0;
                                break;
                            case LEFT:
                            case RIGHT:
                                mLRLevel = 0;
                                break;
                            case FORWARD_LEFT:
                            case FORWARD_RIGHT:
                                mFBLevel = 0;
                                mLRLevel = 0;
                                break;
                            case AUDIO:
                                mBeep = 0;
                                break;
                            case TURN_ROUND:
                                mFBLevel = 0;
                                mLRLevel = 0;
                                stopTurnRound();
                                break;
                            default:
                                break;
                        }
                    }
                }

                sendData();
            } else if (action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_CANCEL) {
                Log.d(LOG_TAG, "ALL UP");
                mFBLevel = 0;
                mLRLevel = 0;
                mBeep = 0;
                stopTurnRound();
                for (TouchArea touchArea : mTouchAreas) {
                    touchArea.release();
                }
                sendData();
            }

        }
        // invalidate();
        return true;
    }

    @Override
    public void sendData() {
        if (mFBLevel > 0) {
            mLed = 1;
        } else {
            mLed = 0;
        }
        super.sendData();
    }
}
